package org.huel.backend.service.impl;

import org.huel.backend.domain.entity.UserRecommendation;
import org.huel.backend.mapper.UserRecommendationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户推荐服务类
 */
@Service
public class UserRecommendationService {

    @Autowired
    private UserRecommendationMapper userRecommendationMapper;

    /**
     * 根据学生ID获取推荐课程（去重处理）
     * @param studentId 学生ID
     * @param limit 限制数量，默认10
     * @return 推荐列表
     */
    public List<UserRecommendation> getRecommendationsByStudentId(String studentId, Integer limit) {
        List<UserRecommendation> allRecommendations = userRecommendationMapper.getRecommendationsByStudentId(studentId, 50);

        // 按课程名去重，保留推荐分数最高的记录
        Map<String, UserRecommendation> courseMap = new LinkedHashMap<>();

        for (UserRecommendation recommendation : allRecommendations) {
            String courseName = recommendation.getCourseName();

            // 如果课程不存在或者当前推荐分数更高，则更新
            if (!courseMap.containsKey(courseName) ||
                    recommendation.getRecommendationScore() > courseMap.get(courseName).getRecommendationScore()) {
                courseMap.put(courseName, recommendation);
            }
        }

        // 转换为列表并按推荐分数排序
        List<UserRecommendation> uniqueRecommendations = new ArrayList<>(courseMap.values());
        uniqueRecommendations.sort((a, b) -> Float.compare(b.getRecommendationScore(), a.getRecommendationScore()));

        // 限制返回数量
        int actualLimit = limit != null ? limit : 10;
        return uniqueRecommendations.stream()
                .limit(actualLimit)
                .collect(Collectors.toList());
    }

    /**
     * 根据推荐类型获取推荐结果（去重处理）
     * @param recommendationType 推荐类型
     * @param limit 限制数量，默认10
     * @return 推荐列表
     */
    public List<UserRecommendation> getRecommendationsByType(String recommendationType, Integer limit) {
        List<UserRecommendation> allRecommendations = userRecommendationMapper.getRecommendationsByType(recommendationType, 50);

        // 按学生ID+课程名组合去重，保留推荐分数最高的记录
        Map<String, UserRecommendation> combinationMap = new LinkedHashMap<>();

        for (UserRecommendation recommendation : allRecommendations) {
            String key = recommendation.getStudentId() + "_" + recommendation.getCourseName();

            if (!combinationMap.containsKey(key) ||
                    recommendation.getRecommendationScore() > combinationMap.get(key).getRecommendationScore()) {
                combinationMap.put(key, recommendation);
            }
        }

        // 转换为列表并按推荐分数排序
        List<UserRecommendation> uniqueRecommendations = new ArrayList<>(combinationMap.values());
        uniqueRecommendations.sort((a, b) -> Float.compare(b.getRecommendationScore(), a.getRecommendationScore()));

        int actualLimit = limit != null ? limit : 10;
        return uniqueRecommendations.stream()
                .limit(actualLimit)
                .collect(Collectors.toList());
    }

    /**
     * 获取所有学生ID列表
     * @return 学生ID列表
     */
    public List<String> getAllStudentIds() {
        return userRecommendationMapper.getAllStudentIds();
    }

    /**
     * 获取所有推荐类型
     * @return 推荐类型列表
     */
    public List<String> getAllRecommendationTypes() {
        return userRecommendationMapper.getAllRecommendationTypes();
    }

    /**
     * 获取热门推荐课程
     * @param limit 限制数量，默认10
     * @return 热门课程列表
     */
    public List<UserRecommendation> getPopularCourses(Integer limit) {
        return userRecommendationMapper.getPopularCourses(limit != null ? limit : 10);
    }



}